home *** CD-ROM | disk | FTP | other *** search
/ Aminet 43 / Aminet 43 (2001)(GTI - Schatztruhe)[!][Jun 2001].iso / Aminet / dev / moni / SystemViewer.lha / Source / GlobalObjects / CommonFuncs.c next >
Encoding:
C/C++ Source or Header  |  2001-04-03  |  44.2 KB  |  1,805 lines

  1. /****h* CommonFuncs.c [2.1] *********************************************
  2. *
  3. * NAME
  4. *    CommonFuncs.c
  5. *
  6. * DESCRIPTION
  7. *    Functions that get used a lot for GUI programming, especially for
  8. *    GadToolsBox-generated GUIs.  This file is used to generate 
  9. *    CommonFuncs.o - a Link-able Object for the SAS-C (V6.58) compiler.
  10. *
  11. *    This a link-able object because there are far too many shared 
  12. *    libraries for the Amiga OS as it is, I couldn't see cluttering 
  13. *    someone's hard drive with another one (especially my own!).
  14. *
  15. * HISTORY
  16. *    03-Apr-2001 - Added the SetupList() function.
  17. *    25-Sep-2000 - Added the GetActiveScreen() function.
  18. *    24-Sep-2000 - Added DisplayTitle() & UserInfo() functions.
  19. *    17-Sep-2000 - Added SetTagPair(), Guarded_FreeLV(), &
  20. *                  Guarded_AllocLV() functions.
  21. *    28-Aug-2000 - Added GetPathName() function.
  22. *    18-May-2000 - Added FontXDim() function.
  23. *    17-May-2000 - Added GetScreenModeID() function.
  24. *    19-Jan-2000 - Changed FileReq() to check for NULL parameter. 
  25. *                  Added File_DirReq() function.
  26. *
  27. * AUTHOR
  28. *    James T. Steichen
  29. *    2217 N. Tamarack Dr.
  30. *    Slayton, Mn. 56172-1155, USA
  31. *    jimbot@rconnect.com
  32. *
  33. * NOTES
  34. *    $VER: CommonFuncs.c 2.1 (03-Apr-2001) by J.T. Steichen
  35. *
  36. * COPYRIGHT
  37. *    CommonFuncs.c & CommonFuncs.h (c) 1999-2001 by J.T. Steichen
  38. *************************************************************************
  39. *
  40. */
  41.  
  42. #include <stdio.h>
  43.  
  44. #include <AmigaDOSErrs.h>
  45.  
  46. #include <exec/types.h>
  47. #include <exec/nodes.h>
  48. #include <exec/lists.h>
  49. #include <exec/memory.h>
  50. #include <exec/libraries.h>
  51.  
  52. #include <libraries/asl.h>
  53. #include <libraries/gadtools.h>
  54.  
  55. #include <intuition/intuitionbase.h>
  56. #include <intuition/intuition.h>
  57.  
  58. #include <graphics/view.h>
  59. #include <graphics/gfxbase.h>
  60. #include <graphics/gfxmacros.h>
  61.  
  62. #include <workbench/workbench.h>
  63.  
  64. #include <dos/dos.h>
  65.  
  66. #include <clib/dos_protos.h>
  67. #include <clib/exec_protos.h>
  68. #include <clib/intuition_protos.h>
  69. #include <clib/graphics_protos.h>
  70.  
  71. #include "CommonFuncs.h"
  72.  
  73. IMPORT __far struct IntuitionBase *IntuitionBase;
  74. IMPORT __far struct GfxBase       *GfxBase;
  75. IMPORT __far struct Library       *GadToolsBase;
  76. IMPORT __far struct Library       *AslBase;
  77.  
  78. // ----------------------------------------------------------------------
  79.  
  80. /****h* SetupList() *****************************************************
  81. *
  82. * NAME
  83. *    SetupList()
  84. *
  85. * SYNOPSIS
  86. *    void SetupList( struct List *lst, struct ListViewMem *lvm );
  87. *
  88. * DESCRIPTION
  89. *    Initialize a List structure for a ListView Gadget.
  90. *************************************************************************
  91. *
  92. */
  93.  
  94. PUBLIC void SetupList( struct List *list, struct ListViewMem *lvm )
  95. {
  96.    int i, len = lvm->lvm_NodeLength;
  97.  
  98.    if (lvm->lvm_NumItems < 256)
  99.       {
  100.       // We can prioritize the nodes:
  101.       for (i = 0; i < lvm->lvm_NumItems; i++)
  102.          {
  103.          lvm->lvm_Nodes[i].ln_Name = &lvm->lvm_NodeStrs[ i * len ];
  104.          lvm->lvm_Nodes[i].ln_Pri  = lvm->lvm_NumItems - i - 129;
  105.          lvm->lvm_Nodes[i].ln_Type = NT_USER;
  106.          }
  107.  
  108.       NewList( list );
  109.  
  110.       for (i = 0; i < lvm->lvm_NumItems; i++)
  111.          Enqueue( list, &(lvm->lvm_Nodes[i]) );
  112.       }
  113.    else
  114.       {
  115.       // Too many nodes to use priorities:
  116.       for (i = 0; i < lvm->lvm_NumItems; i++)
  117.          {
  118.          lvm->lvm_Nodes[i].ln_Name = &lvm->lvm_NodeStrs[ i * len ];
  119.          lvm->lvm_Nodes[i].ln_Pri  = 0;
  120.          lvm->lvm_Nodes[i].ln_Type = NT_USER;
  121.          }
  122.  
  123.       NewList( list );
  124.  
  125.       for (i = 0; i < lvm->lvm_NumItems; i++)
  126.          AddTail( list, &(lvm->lvm_Nodes[i]) );
  127.       }
  128.  
  129.    return;
  130. }
  131.  
  132. /****h* GetActiveScreen() ***********************************************
  133. *
  134. * NAME
  135. *    GetActiveScreen()
  136. *
  137. * SYNOPSIS
  138. *    struct Screen *active = GetActiveScreen( void );
  139. *
  140. * DESCRIPTION
  141. *    Return a pointer to the Active Screen.
  142. *************************************************************************
  143. *
  144. */
  145.  
  146. PUBLIC struct Screen *GetActiveScreen( void )
  147. {
  148.    struct Screen *rval  = NULL;
  149.    ULONG          ilock = 0L;
  150.  
  151.    if (IntuitionBase == NULL)
  152.       return( rval );         // Kill any potential bugs here.
  153.       
  154.    ilock = LockIBase( NULL );
  155.    
  156.       rval = IntuitionBase->ActiveScreen;
  157.       
  158.    UnlockIBase( ilock );
  159.    
  160.    return( rval );     
  161. }
  162.  
  163. /****h* DisplayTitle() **************************************************
  164. *
  165. * NAME
  166. *    DisplayTitle()
  167. *
  168. * SYNOPSIS
  169. *    void DisplayTitle( struct Window *wptr, char *windowTitle );
  170. *
  171. * DESCRIPTION
  172. *    Display the given text as a title for the given Window.
  173. *
  174. * NOTES
  175. *    The title is silently limited to 79 characters in length.
  176. *************************************************************************
  177. *
  178. */
  179.  
  180. PUBLIC void DisplayTitle( struct Window *wptr, char *txt )
  181. {
  182.    static char tbuf[80];
  183.  
  184.    if (txt == NULL)
  185.       return;         // Stop bugs in their tracks!
  186.       
  187.    if (wptr == NULL)
  188.       return;         // Stop bugs in their tracks!
  189.          
  190.    strncpy( &tbuf[0], txt, 79 );
  191.  
  192.    SetWindowTitles( wptr, (UBYTE *) &tbuf[0], (UBYTE *) -1 );
  193.    
  194.    return;
  195. }
  196.  
  197. // ----------------------------------------------------------------------
  198.  
  199. PUBLIC int LVMError = 0; // Error # for Guarded_AllocLV() function.
  200.  
  201. /****h* Guarded_FreeLV() ************************************************
  202. *
  203. * NAME
  204. *    Guarded_FreeLV()
  205. *
  206. * SYNOPSIS
  207. *    void Guarded_FreeLV( struct ListViewMem *lvm );
  208. *
  209. * DESCRIPTION
  210. *    Free the memory associated with a ListView Gadget & reset 
  211. *    the Guard. 
  212. *************************************************************************
  213. *
  214. */
  215.  
  216. PUBLIC void Guarded_FreeLV( struct ListViewMem *lvm )
  217. {
  218.    if (lvm == NULL)
  219.       return;
  220.       
  221.    if (lvm->lvm_NodeStrs != NULL)
  222.       {
  223.       FreeVec( lvm->lvm_NodeStrs );
  224.       lvm->lvm_NodeStrs = NULL;
  225.       }
  226.  
  227.    if (lvm->lvm_Nodes != NULL)
  228.       {
  229.       FreeVec( lvm->lvm_Nodes );
  230.       lvm->lvm_Nodes = NULL;
  231.       }
  232.  
  233.    if (lvm != NULL)
  234.       {
  235.       FreeVec( lvm );
  236.       lvm = NULL;
  237.       }
  238.  
  239.    return;
  240. }
  241.  
  242. /****h* ReportAllocLVError() ********************************************
  243. *
  244. * NAME
  245. *    ReportAllocLVError()
  246. *
  247. * SYNOPSIS
  248. *    void ReportAllocLVError( void );
  249. *
  250. * DESCRIPTION
  251. *    Display a requester informing the User the error that 
  252. *    Guarded_AllocLV() found.
  253. *
  254. * SEE ALSO
  255. *    Guarded_AllocLV(), Guarded_FreeLV()
  256. *************************************************************************
  257. *
  258. */
  259.  
  260. PUBLIC void ReportAllocLVError( void )
  261. {
  262.    IMPORT int LVMError;
  263.  
  264.    char msg[256];
  265.       
  266.    switch (LVMError)
  267.       {
  268.       case LVM_ERROR_NONE:
  269.          sprintf( &msg[0], "No Guarded_AllocLV() error found!" );
  270.          break;
  271.        
  272.       case LVM_ERROR_WRONG_SIZE:
  273.          sprintf( &msg[0], "Guarded_AllocLV() sizes < 1 (Wrong Size!)" );
  274.          break;
  275.          
  276.       case LVM_ERROR_NOMEM:
  277.          sprintf( &msg[0], "Guarded_AllocLV() ran out of Memory!" );
  278.          break;
  279.       } 
  280.  
  281.    UserInfo( &msg[0], "ERROR Report:" );
  282.  
  283.    return;
  284. }
  285.  
  286. /****h* Guarded_AllocLV() ***********************************************
  287. *
  288. * NAME
  289. *    Guarded_AllocLV()
  290. *
  291. * SYNOPSIS
  292. *    struct ListViewMem *lvm = Guarded_AllocLV( int numitems, 
  293. *                                               int itemsize
  294. *                                             );
  295. *
  296. * DESCRIPTION
  297. *    Allocate the memory associated with a ListView Gadget.
  298. *************************************************************************
  299. *
  300. */
  301.  
  302. PUBLIC struct ListViewMem *Guarded_AllocLV( int numitems, int itemsize )
  303. {
  304.    IMPORT int LVMError;
  305.    
  306.    struct ListViewMem *rval = NULL;
  307.    
  308.    if ((numitems < 1) || (itemsize < 1))
  309.       {
  310.       LVMError = LVM_ERROR_WRONG_SIZE;
  311.       
  312.       return( NULL );
  313.       }
  314.  
  315.    // --------- ALLOCATION SECTION: -----------------------------------
  316.  
  317.    rval = (struct ListViewMem *) AllocVec( sizeof( struct ListViewMem ),
  318.                                            MEMF_CLEAR
  319.                                          );  
  320.    
  321.    if (rval == NULL)
  322.       {
  323.       LVMError = LVM_ERROR_NOMEM;
  324.  
  325.       return( NULL );
  326.       }
  327.  
  328.    rval->lvm_Nodes = (struct Node *) 
  329.                       AllocVec( numitems * sizeof( struct Node ),
  330.                                 MEMF_CLEAR 
  331.                               );
  332.  
  333.    if (rval->lvm_Nodes == NULL)
  334.       {
  335.       Guarded_FreeLV( rval );
  336.  
  337.       LVMError = LVM_ERROR_NOMEM;
  338.  
  339.       return( NULL );
  340.       }
  341.  
  342.    rval->lvm_NodeStrs = (UBYTE *) AllocVec( numitems * itemsize, 
  343.                                             MEMF_CLEAR
  344.                                           );
  345.  
  346.    if (rval->lvm_NodeStrs == NULL)
  347.       {
  348.       Guarded_FreeLV( rval );
  349.  
  350.       LVMError = LVM_ERROR_NOMEM;
  351.  
  352.       return( NULL );
  353.       }
  354.  
  355.    // --------- END OF ALLOCATION SECTION: ----------------------------
  356.  
  357.    rval->lvm_NumItems   = numitems;
  358.    rval->lvm_NodeLength = itemsize;   
  359.  
  360.    LVMError = LVM_ERROR_NONE; // Weesa be okey-dokey!
  361.  
  362.    return( rval );
  363. }
  364.  
  365. /****h* GetPathName() ***************************************************
  366. *
  367. * NAME
  368. *    GetPathName()
  369. *  
  370. * SYNOPSIS
  371. *    char *path = GetPathName( char *pathbuf, char *filename, int size );
  372. *
  373. * DESCRIPTION
  374. *    Return with the Path portion of a filename string.
  375. *************************************************************************
  376. *
  377. */
  378.  
  379. PUBLIC char *GetPathName( char *path, char *filename, int size )
  380. {
  381.    int   i;
  382.    char *last = PathPart( filename );
  383.  
  384.    for (i = 0; (i < size) && (filename != last); i++, filename++)
  385.       *(path + i) = *filename;
  386.       
  387.    *(path + i) = '\0';
  388.    
  389.    return( path ); 
  390. }
  391.  
  392.  
  393. /****h* FontXDim() ******************************************************
  394. *
  395. * NAME
  396. *    FontXDim()
  397. *
  398. * SYNOPSIS
  399. *    int length = FontXDim( struct TextAttr *font );
  400. *
  401. * DESCRIPTION
  402. *    Determine the horizontal distance for one character in the given
  403. *    font.
  404. *************************************************************************
  405. *
  406. */
  407.  
  408. PUBLIC int FontXDim( struct TextAttr *font )
  409. {
  410.    struct IntuiText t = { 0, };
  411.     
  412.    t.IText     = " ";
  413.    t.ITextFont = font;
  414.    
  415.    return( IntuiTextLength( &t ) );
  416. }
  417.  
  418. /****h* getScreenModeID() ***********************************************
  419. *
  420. * NAME
  421. *    getScreenModeID()
  422. *
  423. * SYNOPSIS
  424. *    ULONG mode = getScreenModeID( struct TagItem *taglist, 
  425. *                                  struct Screen  *screen,
  426. *                                  char           *req_title
  427. *                                ); 
  428. *
  429. * DESCRIPTION
  430. *    Obtain a user-selected (via ASL) ScreenModeID value.  NULL will
  431. *    be returned if you supply a NULL value for the screen pointer,
  432. *    or if you press & release the Cancel Gadget on the Requester.
  433. *************************************************************************
  434. *
  435. */
  436.  
  437. PUBLIC ULONG getScreenModeID( struct TagItem *taglist, 
  438.                               struct Screen  *scr,
  439.                               char           *title 
  440.                             )
  441. {
  442.    struct ScreenModeRequester *smr = NULL;
  443.  
  444.    ULONG  rval    = NULL;
  445.    BOOL   result  = FALSE;
  446.    BOOL   libflag = FALSE;
  447.    
  448.    if (scr == NULL)
  449.       {
  450.       return( NULL ); // Better safe than sorry!
  451.       }
  452.       
  453.    if (FindTagItem( ASLSM_Screen, taglist ) == NULL)
  454.       {
  455.       return( NULL ); // Missing the ONLY necessary tag!
  456.       }
  457.    else
  458.       SetTagItem( taglist, ASLSM_Screen, (ULONG) scr );
  459.  
  460.    if (FindTagItem( ASLSM_TitleText, taglist ) != NULL)
  461.       SetTagItem( taglist, ASLSM_TitleText, (ULONG) title );
  462.                   
  463.    if ((AslBase = OpenLibrary( AslName, 37L )) != NULL)
  464.       libflag = TRUE;
  465.  
  466.    if (AslBase != NULL)
  467.       {
  468.       smr = (struct ScreenModeRequester *) 
  469.              AllocAslRequest( ASL_ScreenModeRequest, NULL );
  470.  
  471.       if (smr != NULL)
  472.          {
  473.          result = AslRequest( smr, taglist );
  474.  
  475.          if (result == TRUE)
  476.             {
  477.             rval = smr->sm_DisplayID;
  478.             } 
  479.  
  480.          FreeAslRequest( smr );
  481.          }
  482.       else                   // No ScreenModeRequester!
  483.          {
  484.          if (libflag != FALSE)
  485.             CloseLibrary( AslBase );
  486.  
  487.          return( NULL );
  488.          }
  489.  
  490.       if (libflag != FALSE)
  491.          CloseLibrary( AslBase );
  492.       }
  493.    else
  494.       {
  495.       return( NULL );          // AslBase couldn't be opened!
  496.       }
  497.  
  498.    if (result == TRUE)
  499.       return( rval );
  500.    else
  501.       return( NULL );
  502. }
  503.  
  504. /****h* Byt2Str() ***************************************************
  505. *
  506. * NAME
  507. *    Byt2Str()
  508. *
  509. * SYNOPSIS
  510. *    char *hexstr = Byt2Str( char *output, UBYTE input_byte );
  511. *
  512. * DESCRIPTION
  513. *    Take an input binary character & generate a displayable 
  514. *    two-character ASCII 
  515. *    string that displays the HexaDecimal value of each byte.
  516. *
  517. * WARNINGS
  518. *    The output buffer has to be TWO times as long as the input
  519. *    buffer (+ 1 for nil); in other words supply a buffer that's
  520. *    three characters long!
  521. *********************************************************************
  522. *
  523. */
  524.  
  525. PUBLIC char *Byt2Str( char *out, UBYTE input )
  526. {
  527.    UBYTE hexch[] = "0123456789ABCDEF";
  528.  
  529.    UBYTE low     = hexch[  input & 0x0F       ];
  530.    UBYTE high    = hexch[ (input & 0xF0) >> 4 ];
  531.  
  532.    *out       = high;
  533.    *(out + 1) = low;
  534.    *(out + 2) = '\0';
  535.  
  536.    return( out );
  537. }
  538.  
  539. /****h* MakeHexASCIIStr() *******************************************
  540. *
  541. * NAME
  542. *    MakeHexASCIIStr()
  543. *
  544. * SYNOPSIS
  545. *    unsigned int length = MakeHexASCIIStr( char *output, 
  546. *                                           char *input,
  547. *                                           int   inputlength
  548. *                                         );
  549. *
  550. * DESCRIPTION
  551. *    Take an input binary string & generate a displayable ASCII 
  552. *    string that displays the HexaDecimal value of each byte as
  553. *    well as the ASCII representation (for characters >= 0x20 &
  554. *    <= 0x7E).
  555. *
  556. *    Output will be as follows:
  557. *
  558. *    /------------ Hex Bytes ----------\ /--- ASCII ----\
  559. *
  560. *    22334455 22334455 A7223344 7F223344 "3DU"3DU."3D."3D 
  561. *
  562. * WARNINGS
  563. *    The output buffer has to be FOUR times as long as the input
  564. *    buffer!  The maximum value for inlen should be 20 bytes, with
  565. *    16 being the nominal value.
  566. *********************************************************************
  567. *
  568. */
  569.  
  570. PUBLIC unsigned int MakeHexASCIIStr( char *out, char *input, int inlen )
  571. {
  572.    UBYTE hexch[] = "0123456789ABCDEF";
  573.    int   i, j, k;
  574.  
  575.    for (i = 0; i < (4 * inlen); i++)
  576.       *(out + i) = ' ';         // Initialize the output buffer.
  577.  
  578.    // Output the binary as ASCII characters:  
  579.  
  580.    for (i = 0, j = 0, k = 0; i < inlen; i++, j++, k++)
  581.       {
  582.       UBYTE low, high;
  583.          
  584.       low  = hexch[  *(input + i) & 0x0F       ];
  585.       high = hexch[ (*(input + i) & 0xF0) >> 4 ];
  586.  
  587.       *(out + k + j)     = high;
  588.       *(out + k + j + 1) = low;
  589.       
  590.       if ((i + 1) % 4 == 0) // Insert a space between each long word.
  591.          k++;
  592.       }
  593.  
  594.    *(out + k + j) = '\0';
  595.    i = strlen( out );      // compute the start of the ASCII string.
  596.    *(out + k + j) = ' ';
  597.  
  598.    // Output the ASCII representation:   
  599.  
  600.    for (j = 0; j < inlen; j++, i++) 
  601.       {
  602.       if ((*(input + j) < 0x20) || (*(input + j) > 0x7E))
  603.          *(out + i) = 0x2E; /* Output an ASCII period. */
  604.       else
  605.          *(out + i) = *(input + j);
  606.       }
  607.  
  608.    return( (unsigned int) strlen( out ) );
  609. }
  610.  
  611. /****h* File_DirReq() ***************************************************
  612. * NAME
  613. *    File_DirReq()
  614. *
  615. * SYNOPSIS
  616. *    int len = File_DirReq( char           *filename, 
  617. *                           char           *dirname,
  618. *                           struct TagItem *taglist
  619. *                         );
  620. *
  621. * DESCRIPTION
  622. *    Display the ASL file requester with the given taglist.  Return
  623. *    the size of the file selected as well as the filename & Directory.
  624. *
  625. *    The suggested taglist items should be:
  626. *
  627. *    ASLFR_Window,          (ULONG) WindowPointer // definitely needed!
  628. *
  629. *    ASLFR_TitleText,       (ULONG) "Example title..." 
  630. *    ASLFR_InitialHeight,   200,
  631. *    ASLFR_InitialWidth,    400,
  632. *    ASLFR_InitialTopEdge,  16,
  633. *    ASLFR_InitialLeftEdge, 50,
  634. *    ASLFR_PositiveText,    (ULONG) "OKAY!",
  635. *    ASLFR_NegativeText,    (ULONG) "CANCEL!",
  636. *    ASLFR_InitialPattern,  (ULONG) "#?",
  637. *    ASLFR_InitialFile,     (ULONG) "",
  638. *    ASLFR_InitialDrawer,   (ULONG) "RAM:",
  639. *    ASLFR_Flags1,          FRF_DOPATTERNS,
  640. *    ASLFR_Flags2,          FRF_REJECTICONS,
  641. *    ASLFR_SleepWindow,     1,
  642. *    ASLFR_PrivateIDCMP,    1,
  643. *    TAG_END
  644. ************************************************************************
  645. *
  646. */
  647.  
  648. PUBLIC int File_DirReq( char           *filename, 
  649.                         char           *dirname, 
  650.                         struct TagItem *taglist
  651.                       )
  652. {
  653.    struct FileRequester *fr;
  654.  
  655.    int    len, libflag = 0;
  656.  
  657.    if (filename == NULL)
  658.       {
  659.       return( -4 ); // Better safe than sorry!
  660.       }
  661.       
  662.    if (FindTagItem( ASLFR_Window, taglist ) == NULL)
  663.       {
  664.       return( -3 ); // Missing the ONLY necessary tag!
  665.       }
  666.            
  667.    if ((AslBase = OpenLibrary( AslName, 37L )) != NULL)
  668.       libflag = 1;
  669.  
  670.    if (AslBase != NULL)
  671.       {
  672.       fr = (struct FileRequester *) 
  673.                    AllocAslRequest( ASL_FileRequest, taglist );
  674.  
  675.       if (fr != NULL)
  676.          {
  677.  
  678.          if (AslRequest( fr, NULL ))
  679.             {
  680.             (void) strcpy( filename, fr->fr_Drawer );
  681.             len = strlen( filename ) - 1;
  682.  
  683.             if (*(filename + len) == '/' || *(filename + len) == ':')
  684.                (void) strcat( filename, fr->fr_File );
  685.             else
  686.                {      
  687.                // Add a slash to the end of the Path:
  688.                (void) strcat( filename, "/" );
  689.                (void) strcat( filename, fr->fr_File );
  690.                }
  691.             }
  692.  
  693.          if (dirname != NULL)
  694.             strcpy( dirname, fr->fr_Drawer );
  695.  
  696.          FreeAslRequest( fr );
  697.          }
  698.       else                   // No FileRequester!
  699.          {
  700.          if (libflag > 0)
  701.             CloseLibrary( AslBase );
  702.  
  703.          return( -2 );
  704.          }
  705.  
  706.       if (libflag > 0)
  707.          CloseLibrary( AslBase );
  708.       }
  709.    else
  710.       {
  711.       return( -1 );          // AslBase couldn't be opened!
  712.       }
  713.  
  714.    return( strlen( filename ) );
  715. }
  716.  
  717. /****h* FileReq() ******************************************************
  718. * NAME
  719. *    FileReq()
  720. *
  721. * SYNOPSIS
  722. *    int len = FileReq( char *filename, struct TagItem *taglist );
  723. *
  724. * DESCRIPTION
  725. *    Display the ASL file requester with the given taglist.
  726. *
  727. * NOTES
  728. *    This function is identical to File_DirReq() except that we
  729. *    don't use the Directory parameter.
  730. *
  731. *    The suggested taglist items should be:
  732. *
  733. *    ASLFR_Window,          (ULONG) WindowPointer // definitely needed!
  734. *
  735. *    ASLFR_TitleText,       (ULONG) "Example title..." 
  736. *    ASLFR_InitialHeight,   200,
  737. *    ASLFR_InitialWidth,    400,
  738. *    ASLFR_InitialTopEdge,  16,
  739. *    ASLFR_InitialLeftEdge, 50,
  740. *    ASLFR_PositiveText,    (ULONG) "OKAY!",
  741. *    ASLFR_NegativeText,    (ULONG) "CANCEL!",
  742. *    ASLFR_InitialPattern,  (ULONG) "#?",
  743. *    ASLFR_InitialFile,     (ULONG) "",
  744. *    ASLFR_InitialDrawer,   (ULONG) "RAM:",
  745. *    ASLFR_Flags1,          FRF_DOPATTERNS,
  746. *    ASLFR_Flags2,          FRF_REJECTICONS,
  747. *    ASLFR_SleepWindow,     1,
  748. *    ASLFR_PrivateIDCMP,    1,
  749. *    TAG_END
  750. ************************************************************************
  751. *
  752. */
  753.  
  754. PUBLIC int FileReq( char *filename, struct TagItem *taglist )
  755. {
  756.    return( File_DirReq( filename, NULL, taglist ) );
  757. }
  758.  
  759. /* --------------------- User Notification: ------------------------ */
  760.  
  761. PRIVATE struct usernotify {
  762.     
  763.    struct EasyStruct un_ES;
  764.    struct Window     *un_Window;
  765. };
  766.  
  767. PRIVATE struct usernotify userinfo = {
  768.     
  769.    { sizeof( struct EasyStruct ), 0,
  770.      "Program Problem!",
  771.      "Problem (%ld) with Program.\nSelect 'ABORT' to quit:",
  772.      "CONTINUE|ABORT",
  773.    },
  774.  
  775.    NULL
  776. };
  777.  
  778. /****h* SetNotifyWindow() [1.0] *************************************
  779. *
  780. * NAME
  781. *    SetNotifyWindow()
  782. *
  783. * SYNOPSIS
  784. *    (void) SetNotifyWindow( struct Window *wptr );
  785. *
  786. * DESCRIPTION
  787. *    Set the window pointer that Handle_Problem() & other User-
  788. *    Requesters will use for the EasyRequest() call.
  789. *********************************************************************
  790. *
  791. */
  792.  
  793. PUBLIC void SetNotifyWindow( struct Window *wptr )
  794. {
  795.    if (wptr != NULL)
  796.       userinfo.un_Window = wptr;
  797.    
  798.    return;
  799. }
  800.  
  801. /****h* SetReqButtons() [1.0] ***************************************
  802. *
  803. * NAME
  804. *    SetReqButtons
  805. *
  806. * SYNOPSIS
  807. *    (void) SetReqButtons( char *newbuttons );
  808. *
  809. * DESCRIPTION
  810. *    Set the buttons for the Information requester to the given 
  811. *    format string.
  812. *
  813. * NOTES
  814. *    The user of this function should return the buttons to a
  815. *    known string after Handle_Problem() or the other User-
  816. *    requesters are called.
  817. *********************************************************************
  818. *
  819. */
  820.  
  821. PUBLIC void SetReqButtons( char *newbuttons )
  822. {
  823.    if (newbuttons != NULL)
  824.       userinfo.un_ES.es_GadgetFormat = newbuttons;
  825.    else
  826.       userinfo.un_ES.es_GadgetFormat = "CONTINUE|ABORT!";
  827.       
  828.    return;
  829. }
  830.  
  831. /****i* NotifyUser() [1.0] ******************************************
  832. *
  833. * NAME
  834. *    NotifyUser() - Internal to CommonFuncs.o only (PRIVATE!)
  835. *
  836. * SYNOPSIS
  837. *    int ans = NotifyUser( char          *info, 
  838. *                          char          *title,
  839. *                          struct Window *wptr, 
  840. *                          int           *errnum
  841. *                        );
  842. *
  843. * DESCRIPTION
  844. *    Get a response from the user.
  845. *
  846. * FUNCTION
  847. *    Return -1 if the user selected the far-right button (ABORT?),
  848. *    else return 0 (CONTINUE?).
  849. *
  850. * INPUTS
  851. *    info   - Information string for the user to act on.
  852. *    title  - Title of the Problem Requester.
  853. *    wptr   - The window to open the Requester on.
  854. *    errnum - Optional error number (normally NULL).
  855. *********************************************************************
  856. *
  857. */
  858.  
  859. PRIVATE int NotifyUser( char          *problem,
  860.                         char          *title,
  861.                         struct Window *wptr,
  862.                         int           errnum
  863.                       )
  864. {
  865.    int rval = 0, answer = 0;
  866.    int oldflags, newflags = wptr->IDCMPFlags;
  867.    
  868.    userinfo.un_ES.es_TextFormat = problem;
  869.    userinfo.un_ES.es_Title      = title;
  870.    oldflags                     = newflags;
  871.    
  872.    // Turn off verify IDCMP messages:
  873.    newflags &= ~(IDCMP_SIZEVERIFY | IDCMP_REQVERIFY | IDCMP_MENUVERIFY);
  874.  
  875.    ModifyIDCMP( wptr, newflags );
  876.    
  877.    answer = EasyRequest( wptr, &userinfo.un_ES, NULL, errnum );
  878.  
  879.    switch (answer)
  880.       {
  881.       case 1:
  882.          rval = RETURN_OK; // Continue Button.
  883.          break;
  884.          
  885.       case 0:        // This is the far right button!
  886.          rval = -1;
  887.          break;
  888.       }
  889.  
  890.    ModifyIDCMP( wptr, oldflags ); // Restore old IDCMP flags.
  891.    return( rval );
  892. }
  893.  
  894. /****h* Handle_Problem() [1.0] **************************************
  895. *
  896. * NAME
  897. *    Handle_Problem()
  898. *
  899. * SYNOPSIS
  900. *    int ans = Handle_Problem( char *info, char *title, int *errnum );
  901. *
  902. * DESCRIPTION
  903. *    Get a Yes/No Response from the user.
  904. *
  905. * FUNCTION
  906. *    Return -1 if the user selected the ABORT (far-right) button,
  907. *    else return the errnum value (normally 0).
  908. *
  909. * INPUTS
  910. *    info   - Information string for the user to act on.
  911. *    title  - Title of the Problem Requester.
  912. *    errnum - Optional error number (normally NULL).
  913. *
  914. * NOTES
  915. *    Be sure to call SetNotifyWindow( wptr ) BEFORE using this 
  916. *    function for the first time!
  917. *********************************************************************
  918. *
  919. */
  920.  
  921. PUBLIC int Handle_Problem( char *info, char *title, int *errnum )
  922. {
  923.    int errornum = 0;
  924.    
  925.    if (errnum != NULL)
  926.       errornum = *errnum;
  927.       
  928.    if (userinfo.un_Window == NULL)
  929.       return( -2 );
  930.       
  931.    if (NotifyUser( info, title, userinfo.un_Window, errornum ) == -1)
  932.       return( -1 );
  933.    else
  934.       return( errornum );  // User didn't press the ABORT button!
  935. }
  936.  
  937. /****h* GetUserResponse() [1.0] *************************************
  938. *
  939. * NAME
  940. *    GetUserResponse - Get a button response from the user.
  941. *
  942. * SYNOPSIS
  943. *    int ans = GetUserResponse( char *info, char *title, int *errnum );
  944. *
  945. * FUNCTION
  946. *    Return -1 if there's no window, otherwise return the ordinal
  947. *    of the button the user pressed.  This means that there can be
  948. *    more than two choice buttons for the User to choose from.
  949. *
  950. * INPUTS
  951. *    info   - Information string for the user to act on.
  952. *    title  - Title of the Problem Requester.
  953. *    errnum - Optional error number.
  954. *
  955. * NOTES
  956. *    GetUserResponse() will return 0 for the right-most button,
  957. *    instead of n, where n is the number of buttons.  This is the
  958. *    behavior of the EasyRequest() function (so don't blame me!).
  959. *
  960. *    Call SetReqButtons() before using this function &
  961. *    be sure to call SetNotifyWindow( wptr ) BEFORE using this 
  962. *    function for the first time!
  963. *********************************************************************
  964. *
  965. */
  966.  
  967. PUBLIC int  GetUserResponse( char *problem, char *title, int *errnum )
  968. {
  969.    struct Window *wptr = userinfo.un_Window;
  970.    int            answer = 0;
  971.    int            oldflags, newflags, errornum;
  972.    
  973.    if (wptr == NULL)
  974.       return( -1 );
  975.       
  976.    if (errnum != NULL)
  977.       errornum = *errnum;
  978.       
  979.    newflags                     = wptr->IDCMPFlags;
  980.    userinfo.un_ES.es_TextFormat = problem;
  981.    userinfo.un_ES.es_Title      = title;
  982.    oldflags                     = newflags;
  983.    
  984.    // Turn off verify IDCMP messages:
  985.    newflags &= ~(IDCMP_SIZEVERIFY | IDCMP_REQVERIFY | IDCMP_MENUVERIFY);
  986.  
  987.    ModifyIDCMP( wptr, newflags );
  988.    
  989.    answer = EasyRequest( wptr, &userinfo.un_ES, NULL, errnum );
  990.  
  991.    ModifyIDCMP( wptr, oldflags ); // Restore old IDCMP flags.
  992.  
  993.    return( answer );
  994.    
  995. }
  996.  
  997. /****h* SanityCheck() [1.0] **************************************
  998. *
  999. * NAME
  1000. *    SanityCheck - Get a Yes/No Response from the user.
  1001. *
  1002. * SYNOPSIS
  1003. *    Boolean answer = SanityCheck( char *question );
  1004. *
  1005. * FUNCTION
  1006. *    Return TRUE if the User pressed the 'YES' button.
  1007. *    Return FALSE if the User pressed the 'NO' button.
  1008. *
  1009. * INPUTS
  1010. *    question - question the User has to respond to.
  1011. *
  1012. * NOTES
  1013. *    Be sure to call SetNotifyWindow( wptr ) BEFORE using this 
  1014. *    function for the first time!
  1015. ******************************************************************
  1016. *
  1017. */
  1018.  
  1019. PUBLIC BOOL SanityCheck( char *question )
  1020. {
  1021.    BOOL rval = FALSE;
  1022.  
  1023.    SetReqButtons( "YES|NO" );   
  1024.  
  1025.    rval = Handle_Problem( question, "User SANITY CHECK:", NULL );
  1026.  
  1027.    SetReqButtons( "CONTINUE|ABORT" );   
  1028.  
  1029.    if (rval == 0)
  1030.       rval = TRUE;
  1031.    else 
  1032.       rval = FALSE;
  1033.       
  1034.    return( rval ); 
  1035. }
  1036.  
  1037. /****h* UserInfo() [1.0] *****************************************
  1038. *
  1039. * NAME
  1040. *    UserInfo()
  1041. *
  1042. * SYNOPSIS
  1043. *    void UserInfo( char *message, char *windowtitle );
  1044. *
  1045. * DESCRIPTION
  1046. *    Tell user some information.
  1047. *
  1048. * NOTES
  1049. *    Be sure to call SetNotifyWindow( wptr ) BEFORE using this 
  1050. *    function for the first time!
  1051. ******************************************************************
  1052. *
  1053. */
  1054.  
  1055. PUBLIC void UserInfo( char *msg, char *title )
  1056. {
  1057.    SetReqButtons( "OKAY" );   
  1058.  
  1059.    (void) Handle_Problem( msg, title, NULL );
  1060.  
  1061.    SetReqButtons( "CONTINUE|ABORT" );   
  1062.  
  1063.    return; 
  1064.  
  1065. /****h* ComputeX() [1.0] ********************************************
  1066. *
  1067. * NAME
  1068. *    ComputeX()
  1069. *
  1070. * SYNOPSIS
  1071. *    UWORD size = ComputeX( UWORD fontxsize, UWORD value );
  1072. *
  1073. * DESCRIPTION
  1074. *    This function returns (fontxsize * value + 4) / 8
  1075. *
  1076. * NOTES
  1077. *    This function will probably be changed to private later.
  1078. *********************************************************************
  1079. *
  1080. */
  1081.  
  1082. PUBLIC UWORD ComputeX( UWORD fontxsize, UWORD value )
  1083. {
  1084.    return( (UWORD) (((fontxsize * value) + 4) / 8) );
  1085. }
  1086.  
  1087. /****h* ComputeY() [1.0] ********************************************
  1088. *
  1089. * NAME
  1090. *    ComputeY()
  1091. *
  1092. * SYNOPSIS
  1093. *    UWORD size = ComputeY( UWORD fontysize, UWORD value );
  1094. *
  1095. * DESCRIPTION
  1096. *    This function returns (fontysize * value + 4) / 8
  1097. *
  1098. * NOTES
  1099. *    This function will probably be changed to private later.
  1100. *********************************************************************
  1101. *
  1102. */
  1103.  
  1104. PUBLIC UWORD ComputeY( UWORD fontysize, UWORD value )
  1105. {
  1106.    return( (UWORD) (((fontysize * value) + 4) / 8) );
  1107. }
  1108.  
  1109. /****h* ComputeFont() [1.0] *****************************************
  1110. *
  1111. * NAME
  1112. *    ComputeFont()
  1113. *
  1114. * SYNOPSIS
  1115. *    void ComputeFont( struct Screen   *screen,
  1116. *                      struct TextAttr *font,
  1117. *                      struct CompFont *cfont,
  1118. *                      UWORD            width,
  1119. *                      UWORD            height
  1120. *                    );
  1121. *
  1122. * DESCRIPTION
  1123. *    Initialize the CompFont structure according to the supplied
  1124. *    values in the font, width & height.  If the results won't fit
  1125. *    the screen dimensions, use topaz (8) in the computations
  1126. *    instead.
  1127. *
  1128. * NOTES
  1129. *    GfxBase has to be open BEFORE calling this function!
  1130. *********************************************************************
  1131. *
  1132. */
  1133.  
  1134. PUBLIC void ComputeFont( struct Screen   *Scr, 
  1135.                          struct TextAttr *Font,
  1136.                          struct CompFont *cf,
  1137.                          UWORD            width, 
  1138.                          UWORD            height
  1139.                        )
  1140. {
  1141.    if (GfxBase == NULL)
  1142.       goto UseTopaz;
  1143.       
  1144.    Forbid(); // ---------------------------------------------
  1145.      
  1146.      Font->ta_Name  = (STRPTR) 
  1147.                       GfxBase->DefaultFont->tf_Message.mn_Node.ln_Name;
  1148.      
  1149.      Font->ta_YSize = GfxBase->DefaultFont->tf_YSize;
  1150.      
  1151.      cf->FontY = GfxBase->DefaultFont->tf_YSize; 
  1152.      cf->FontX = GfxBase->DefaultFont->tf_XSize; 
  1153.      
  1154.    Permit(); // ---------------------------------------------
  1155.  
  1156.    cf->OffX = Scr->WBorLeft;
  1157.    cf->OffY = Scr->RastPort.TxHeight + Scr->WBorTop + 1;
  1158.  
  1159.    if ((width != 0) && (height != 0))
  1160.       {
  1161.       if ((ComputeX( cf->FontX, width ) 
  1162.            + cf->OffX + Scr->WBorRight) > Scr->Width)
  1163.          goto UseTopaz;
  1164.          
  1165.       if ((ComputeY( cf->FontY, height ) 
  1166.            + cf->OffY + Scr->WBorBottom) > Scr->Height)
  1167.          goto UseTopaz;
  1168.       }   
  1169.  
  1170.    return;
  1171.    
  1172. UseTopaz:
  1173.  
  1174.    Font->ta_Name  = (STRPTR) "topaz.font";
  1175.    Font->ta_YSize = 8;
  1176.    
  1177.    cf->FontX      = 8;
  1178.    cf->FontY      = 8;
  1179.  
  1180.    // These might be in error:
  1181.    cf->OffX       = Scr->WBorLeft;
  1182.    cf->OffY       = Scr->RastPort.TxHeight + Scr->WBorTop + 1;
  1183.  
  1184.    return;
  1185. }
  1186.  
  1187. /****h* FindTools() [1.0] *******************************************
  1188. *
  1189. * NAME
  1190. *    FindTools()
  1191. * DESCRIPTION
  1192. *    Locate a tools array for a given name.
  1193. *
  1194. * SYNOPSIS
  1195. *    char **toolarray = FindTools( struct DiskObject *diskobj,
  1196. *                                  char              *filename,
  1197. *                                  BPTR               directory_lock
  1198. *                                );
  1199. *
  1200. * INPUTS
  1201. *    filename = null-terminated name of file to look for.
  1202. *    lock     = directory to look for file in.
  1203. *    diskobj  = storage (for a later call to FreeDiskObject()) 
  1204. *
  1205. * WARNINGS
  1206. *    Be sure to call FreeDiskObject() later in your program.
  1207. *    FindTools() doesn't do this because it doesn't know what 
  1208. *    you're going to do with the ToolTypes. 
  1209. *********************************************************************
  1210. *
  1211. */
  1212.  
  1213. PUBLIC char **FindTools( struct DiskObject *diskobj, 
  1214.                          char              *name, 
  1215.                          BPTR               lock 
  1216.                        )
  1217. {
  1218.    BPTR   olddir = NULL;
  1219.    char **tools  = NULL;
  1220.  
  1221.    if (lock == NULL)
  1222.       return( NULL );
  1223.       
  1224.    olddir = CurrentDir( lock );
  1225.    
  1226.    if ((diskobj = (struct DiskObject *) GetDiskObject( name )) != NULL)
  1227.       tools = diskobj->do_ToolTypes;
  1228.    
  1229.    (void) CurrentDir( olddir );
  1230.  
  1231.    return( tools );
  1232. }
  1233.  
  1234. /****h* GetToolStr() [1.0] ******************************************
  1235. *
  1236. * NAME
  1237. *    GetToolStr()
  1238. *
  1239. * DESCRIPTION
  1240. *    Find the tooltype string that matches the given name.
  1241. *
  1242. * SYNOPSIS
  1243. *    toolstring = GetToolStr( char **toolarray,
  1244. *                             char  *toolname,
  1245. *                             char  *default_tool
  1246. *                           );
  1247. *
  1248. * INPUTS
  1249. *    toolarray    = pointer from FindTools() call.
  1250. *    toolname     = tool we're searching the icon for.
  1251. *    default_tool = default string to return if the tool isn't found.
  1252. *********************************************************************
  1253. *
  1254. */
  1255.  
  1256. PUBLIC char *GetToolStr( char **toolptr, char *name, char *deflt )
  1257. {
  1258.    char *found = NULL;
  1259.    
  1260.    found = (char *) FindToolType( toolptr, name );
  1261.  
  1262.    return( (found != NULL) ? found : deflt );
  1263. }
  1264.  
  1265. /****h* GetToolInt() [1.0] ******************************************
  1266. *
  1267. * NAME
  1268. *    GetToolInt() 
  1269. *
  1270. * DESCRIPTION
  1271. *    Find the tooltype integer that matches the given name.
  1272. *
  1273. * SYNOPSIS
  1274. *    tool_int = GetToolInt( char **toolarray,
  1275. *                           char  *toolname,
  1276. *                           int    default_val
  1277. *                         );
  1278. *
  1279. * INPUTS
  1280. *    toolarray   = pointer from FindTools() call.
  1281. *    toolname    = tool we're searching the icon for.
  1282. *    default_val = default integer to return if the tool isn't found.
  1283. *********************************************************************
  1284. *
  1285. */
  1286.  
  1287. PUBLIC int GetToolInt( char **toolptr, char *name, int defaultvalue )
  1288. {
  1289.    char *found = NULL;
  1290.    int   rval  = 0;
  1291.    
  1292.    found = (char *) FindToolType( toolptr, name );
  1293.  
  1294.    rval  = atoi( found );
  1295.  
  1296.    return( (rval > 0) ? rval : defaultvalue );
  1297. }
  1298.  
  1299. /****h* GetToolBoolean() [1.0] **************************************
  1300. *
  1301. * NAME
  1302. *    GetToolBoolean()
  1303. *
  1304. * DESCRIPTION
  1305. *    Find the tooltype boolean that matches the given name.
  1306. *
  1307. * SYNOPSIS
  1308. *    boolean ans = GetToolBoolean( char **toolptr,
  1309. *                                  char  *name,
  1310. *                                  int    defaultBool
  1311. *                                );
  1312. *
  1313. * INPUTS
  1314. *    toolptr     = pointer from FindTools() call.
  1315. *    name        = tool we're searching the icon for.
  1316. *    defaultBool = default boolean to return if the tool isn't found.
  1317. *
  1318. * NOTES
  1319. *    The following strings will return a Boolean value of TRUE:
  1320. *        
  1321. *        "TRUE", "YES", "OK" & "OKAY"
  1322. *
  1323. *    any other value will return FALSE.
  1324. *********************************************************************
  1325. *
  1326. */
  1327.  
  1328. PUBLIC BOOL GetToolBoolean( char **toolptr, char *name, int defaultBool )
  1329. {
  1330.    char *found = NULL;
  1331.    
  1332.    found = (char *) FindToolType( toolptr, name );
  1333.  
  1334.    if (found != NULL)
  1335.       {
  1336.       if (strcmp( found, "TRUE" ) == 0)
  1337.          return( TRUE );
  1338.       else if (strcmp( found, "YES"  ) == 0)
  1339.          return( TRUE );
  1340.       else if (strcmp( found, "OK"   ) == 0)
  1341.          return( TRUE );
  1342.       else if (strcmp( found, "OKAY" ) == 0)
  1343.          return( TRUE );
  1344.       else
  1345.          return( FALSE );
  1346.       }
  1347.    else
  1348.       return( (BOOL) defaultBool );
  1349. }
  1350.  
  1351. /****h* FindIcon() [1.0] ********************************************
  1352. *
  1353. * NAME
  1354. *    FindIcon()
  1355. *
  1356. * DESCRIPTION
  1357. *    Find the icon associated with pgmname (if any) & process the
  1358. *    ToolTypes array in the icon with ToolProc().
  1359. *
  1360. * SYNOPSIS
  1361. *    void *rval = FindIcon( void              *(ToolProc)(char **), 
  1362. *                           struct DiskObject *dobj,
  1363. *                           char              *pgmname
  1364. *                         );
  1365. *
  1366. * INPUTS
  1367. *    ToolProc() - a Pointer to a function that will perform an
  1368. *                 operation on the ToolTypes array from the Icon
  1369. *                 if the icon was found.
  1370. *
  1371. *    dobj       - storage (for a later call to FreeDiskObject())
  1372. *
  1373. *    pgmname    - The name of the icon to look for (without the
  1374. *                 ".info" at the end of the string). 
  1375. *
  1376. * NOTES
  1377. *    If the user starts a program from the CLI, & the icon is in
  1378. *    the same directory, we will get the ToolTypes from the icon
  1379. *    instead of using the built-in defaults.
  1380. *********************************************************************
  1381. *
  1382. */
  1383.  
  1384. PUBLIC void *FindIcon( void              *(ToolProc)(char **), 
  1385.                        struct DiskObject *dobj,
  1386.                        char              *pgmname
  1387.                      )
  1388. {
  1389.    BPTR   dirlock = NULL;
  1390.    char **toolptr = NULL;
  1391.    void  *rval    = NULL;
  1392.    
  1393.    dirlock = GetProgramDir(); // AmigaDOS function.
  1394.    
  1395.    if (dirlock != NULL)
  1396.       {
  1397.       toolptr = FindTools( dobj, pgmname, dirlock );
  1398.       rval    = ToolProc( toolptr ); // Do something with the tools!
  1399.       }
  1400.  
  1401.    return( rval );
  1402. }
  1403.  
  1404. /****h* CloseLibs() [1.0] *******************************************
  1405. *
  1406. * NAME
  1407. *    CloseLibs()
  1408. *
  1409. * SYNOPSIS
  1410. *    void CloseLibs( void );
  1411. *
  1412. * DESCRIPTION
  1413. *    Close the three most-commonly used libraries - 
  1414. *
  1415. *       intuition.library, graphics.library & gadtools.library.
  1416. *********************************************************************
  1417. *
  1418. */
  1419.  
  1420. PUBLIC void CloseLibs( void )
  1421. {
  1422.    if (IntuitionBase != NULL)
  1423.       CloseLibrary( (struct Library *) IntuitionBase );
  1424.  
  1425.    if (GfxBase != NULL)
  1426.       CloseLibrary( (struct Library *) GfxBase );
  1427.  
  1428.    if (GadToolsBase != NULL)
  1429.       CloseLibrary( GadToolsBase );
  1430.       
  1431.    return;
  1432. }
  1433.  
  1434. /****h* OpenLibs() [1.0] ********************************************
  1435. *
  1436. * NAME
  1437. *    OpenLibs()
  1438. *
  1439. * SYNOPSIS
  1440. *    int result = OpenLibs( void );
  1441. *
  1442. * DESCRIPTION
  1443. *    Open the three most-commonly used libraries (V39+) - 
  1444. *
  1445. *       intuition.library, graphics.library & gadtools.library.
  1446. *
  1447. * RETURN VALUE
  1448. *    Negative integer if a library could NOT be opened, zero if
  1449. *    all was successful.
  1450. *********************************************************************
  1451. *
  1452. */
  1453.  
  1454. PUBLIC int OpenLibs( void )
  1455. {
  1456.    IntuitionBase = (struct IntuitionBase *)
  1457.                    OpenLibrary( "intuition.library", 39L );
  1458.  
  1459.    if (IntuitionBase == NULL)
  1460.       return( -1 );
  1461.  
  1462.    GfxBase = (struct GfxBase *)
  1463.              OpenLibrary( "graphics.library", 39L );
  1464.  
  1465.    if (GfxBase == NULL)
  1466.       {
  1467.       CloseLibs();
  1468.       return( -2 );
  1469.       }
  1470.  
  1471.    GadToolsBase = OpenLibrary( "gadtools.library", 39L );
  1472.  
  1473.    if (GadToolsBase == NULL)
  1474.       {
  1475.       CloseLibs();
  1476.       return( -3 );
  1477.       }
  1478.  
  1479.    return( 0 );
  1480. }
  1481.  
  1482. /****h* RGB2HSV() ***************************************************
  1483. *
  1484. * NAME
  1485. *    RGB2HSV()
  1486. *
  1487. * SYNOPSIS
  1488. *    struct ColorCoords *ans = RGB2HSV( struct ColorCoords *input );
  1489. *
  1490. * DESCRIPTION
  1491. *    Convert an RGB color coordinate set into HSV (Hue,
  1492. *    Saturation & Luminance).
  1493. *
  1494. * NOTES
  1495. *    red, green & blue are values from 0 to 255 (normally).
  1496. *
  1497. * WARNINGS
  1498. *    If you want the input structure for later, save it before
  1499. *    passing it to this function, since it modifies it & returns it.
  1500. *********************************************************************
  1501. *
  1502. */
  1503.  
  1504. PUBLIC struct ColorCoords *RGB2HSV( struct ColorCoords *input )
  1505. {
  1506.    float hue, saturation, luminance;
  1507.    float red, green, blue;
  1508.    float ma, mi, d;
  1509.    
  1510.    red   = (float) input->Red_Hue          / 15;
  1511.    green = (float) input->Green_Saturation / 15;
  1512.    blue  = (float) input->Blue_Luminance   / 15;
  1513.  
  1514.    ma = (red > blue)  ? red : blue;
  1515.    ma = (ma  > green) ? ma  : green;
  1516.    mi = (red < blue)  ? red : blue;
  1517.    mi = (mi  < green) ? mi  : green;
  1518.  
  1519.    luminance = ma;
  1520.    
  1521.    if (ma != 0)
  1522.       saturation = (ma - mi) / ma;
  1523.    else
  1524.       saturation = 0;
  1525.       
  1526.    if (saturation == 0)
  1527.       hue = 0;
  1528.    else
  1529.       {
  1530.       d = ma - mi;
  1531.       
  1532.       if (red == ma)
  1533.          hue = (green - blue) / d;
  1534.       else if (green == ma)
  1535.          hue = 2 + (blue - red) / d;
  1536.       else
  1537.          hue = 4 + (red - green) / d;
  1538.  
  1539.       hue *= 60;
  1540.       
  1541.       if (hue < 0)
  1542.          hue += 360;
  1543.       }
  1544.  
  1545.    input->Red_Hue          = (LONG) hue;
  1546.    input->Green_Saturation = (LONG) 100 * saturation;
  1547.    input->Blue_Luminance   = (LONG) 100 * luminance;
  1548.  
  1549.    return( input );
  1550. }
  1551.  
  1552. /****h* HSV2RGB() ***************************************************
  1553. *
  1554. * NAME
  1555. *    HSV2RGB()
  1556. *
  1557. * SYNOPSIS
  1558. *    struct ColorCoords *ans = HSV2RGB( struct ColorCoords *input );
  1559. *
  1560. * DESCRIPTION 
  1561. *    Convert an HSV (Hue, Saturation & Luminance) coordinate into
  1562. *    RGB (red, green & blue).
  1563. *
  1564. * NOTES
  1565. *    red, green & blue are values from 0 to 255 (normally).
  1566. *
  1567. * WARNINGS
  1568. *    If you want the input structure for later, save it before
  1569. *    passing it to this function, since it modifies it & returns it.
  1570. *
  1571. *********************************************************************
  1572. *
  1573. */
  1574.  
  1575. PUBLIC struct ColorCoords *HSV2RGB( struct ColorCoords *input )
  1576. {
  1577.    float hue, saturation, luminance;
  1578.    float red, green, blue;
  1579.    float p1, p2, p3, f;
  1580.    int   i = 0;
  1581.  
  1582.   hue        = (float) input->Red_Hue          / 60;
  1583.   luminance  = (float) input->Blue_Luminance   / 100;
  1584.   saturation = (float) input->Green_Saturation / 100;
  1585.  
  1586.   while ((f = hue - i) > 1)
  1587.     i++;
  1588.  
  1589.   p1 = luminance * (1 - saturation);
  1590.   p2 = luminance * (1 - (saturation * f));
  1591.   p3 = luminance * (1 - (saturation * (1 - f)));
  1592.  
  1593.   switch (i)
  1594.     {
  1595.     case 0:
  1596.       red   = luminance;
  1597.       green = p3;
  1598.       blue  = p1;
  1599.       break;
  1600.  
  1601.     case 1:
  1602.       red   = p2;
  1603.       green = luminance;
  1604.       blue  = p1;
  1605.       break;
  1606.  
  1607.     case 2:
  1608.       red   = p1;
  1609.       green = luminance;
  1610.       blue  = p3;
  1611.       break;
  1612.  
  1613.     case 3:
  1614.       red   = p1;
  1615.       green = p2;
  1616.       blue  = luminance;
  1617.       break;
  1618.  
  1619.     case 4:
  1620.       red   = p3;
  1621.       green = p1;
  1622.       blue  = luminance;
  1623.       break;
  1624.  
  1625.     case 5:
  1626.       red   = luminance;
  1627.       green = p1;
  1628.       blue  = p2;
  1629.       break;
  1630.     }
  1631.  
  1632.   input->Red_Hue          = (UWORD) (red   * 15);
  1633.   input->Green_Saturation = (UWORD) (green * 15);
  1634.   input->Blue_Luminance   = (UWORD) (blue  * 15);
  1635.  
  1636.   return( input );
  1637.  
  1638. }
  1639.  
  1640. /****h* SetTagItem() [1.0] ******************************************
  1641. *
  1642. * NAME
  1643. *    SetTagItem()
  1644. *
  1645. * SYNOPSIS
  1646. *    void SetTagItem( struct TagItem *taglist, ULONG tag, ULONG value );
  1647. *
  1648. * DESCRIPTION
  1649. *    This function searches the given taglist for the given tag.  If
  1650. *    the tag is found, its value (ti_Data) is changed to the given
  1651. *    value.  This function completes the functionality for using 
  1652. *    TagLists.
  1653. *********************************************************************
  1654. *
  1655. */
  1656.  
  1657. PUBLIC void SetTagItem( struct TagItem *taglist, ULONG tag, ULONG value )
  1658. {
  1659.    struct TagItem *item = (struct TagItem *) FindTagItem( tag, taglist );
  1660.    
  1661.    if (item != NULL)
  1662.       item->ti_Data = value;
  1663.    
  1664.    return;
  1665. }
  1666.  
  1667. /****h* SetTagPair() [1.0] ******************************************
  1668. *
  1669. * NAME
  1670. *    SetTagPair()
  1671. *
  1672. * SYNOPSIS
  1673. *    void SetTagPair( struct TagItem *taglist, ULONG tag, ULONG value );
  1674. *
  1675. * DESCRIPTION
  1676. *    Add a Tag & value to a TagItem list.  If taglist is NULL, 
  1677. *    nothing is done by this function.
  1678. *    This function completes the functionality for using TagLists.
  1679. *
  1680. * NOTES
  1681. *    taglist is really an array of ULONG values organized into pairs.
  1682. *
  1683. * WARNINGS
  1684. *    No check is done to see if there is space in the Tag list
  1685. *    for the added pair (so why not reserve space by using: 
  1686. *    {TAG_IGNORE, NULL} in the taglist you provide?).
  1687. *********************************************************************
  1688. *
  1689. */
  1690.  
  1691. PUBLIC void SetTagPair( struct TagItem *taglist, ULONG tag, ULONG value )
  1692. {
  1693.    if (taglist == NULL)
  1694.       return;            // Do NOT cause Enforcer hits.
  1695.       
  1696.    taglist->ti_Tag  = tag;
  1697.    taglist->ti_Data = value;   
  1698.  
  1699.    return;
  1700. }
  1701.  
  1702. /****h* FGetS() [1.0] **********************************************
  1703. *
  1704. * NAME
  1705. *    FGetS()
  1706. *
  1707. * SYNOPSIS
  1708. *    char *str = FGetS( char *buffer, int readlength, FILE *fptr );
  1709. *
  1710. * DESCRIPTION
  1711. *    This function is identical to the C library function fgets()
  1712. *    except that it changes the \n at the end of a line to a \0.
  1713. ********************************************************************
  1714. *
  1715. */
  1716.  
  1717. PUBLIC char *FGetS( char *buffer, int length, FILE *fileptr )
  1718. {
  1719.    char *rval = NULL, *cp = NULL;
  1720.    int   len  = 0;
  1721.    
  1722.    rval = fgets( buffer, length, fileptr );
  1723.    
  1724.    if (rval != NULL)
  1725.       {
  1726.       len = strlen( buffer ) - 1;
  1727.       
  1728.       cp = &buffer[ len ];
  1729.       
  1730.       if (*cp == '\n')
  1731.          *cp = '\0';
  1732.       }
  1733.  
  1734.    return( rval );
  1735. }
  1736.  
  1737. /****h* HideListFromView() ******************************************
  1738. *
  1739. * NAME
  1740. *    HideListFromView()
  1741. *
  1742. * SYNOPSIS
  1743. *    void HideListFromView( struct Gadget *listview_gadget,
  1744. *                           struct Window *window_pointer
  1745. *                         );
  1746. *
  1747. * DESCRIPTION
  1748. *    Turn off the Given ListView Gadget for the window, so that it
  1749. *    can be modified elsewhere.
  1750. *
  1751. * SEE ALSO
  1752. *    ModifyListView()
  1753. *********************************************************************
  1754. *
  1755. */ 
  1756.  
  1757. PUBLIC void HideListFromView( struct Gadget *lv, struct Window *w )
  1758. {
  1759.    GT_SetGadgetAttrs( lv, w, NULL, GTLV_Labels, ~0, TAG_DONE );
  1760.    return;
  1761. }
  1762.  
  1763. /****h* ModifyListView() ********************************************
  1764. *
  1765. * NAME
  1766. *    ModifyListView()
  1767. *
  1768. * SYNOPSIS
  1769. *    void ModifyLsitView( struct Gadget *listview_gadget,
  1770. *                         struct Window *window_pointer,
  1771. *                         struct List   *listview_contents,
  1772. *                         struct Gadget *string_gadget
  1773. *                       ); 
  1774. *
  1775. * DESCRIPTION
  1776. *    Change the Given ListView Gadget for the window to the 
  1777. *    new parameters, which will re-display it.
  1778. *
  1779. * SEE ALSO
  1780. *    HideListFromView()
  1781. *********************************************************************
  1782. *
  1783. */ 
  1784.  
  1785. PUBLIC void ModifyListView( struct Gadget *lv, 
  1786.                             struct Window *w,
  1787.                             struct List   *list,
  1788.                             struct Gadget *strgadget
  1789.                           )
  1790. {
  1791.    GT_SetGadgetAttrs( lv, w, NULL,
  1792.                       GTLV_Labels,       list,
  1793.                       GTLV_ShowSelected, strgadget,
  1794.                       GTLV_Selected,     0,
  1795.                       TAG_DONE
  1796.                     );
  1797.    return;
  1798. }
  1799.  
  1800. /* ---------------- END of CommonFuncs.c file! ----------------------- */
  1801.